/*
 * Copyright 2013-2018 Lilinfeng.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.study.guide.privateProtocol.struct;

import java.util.HashMap;
import java.util.Map;

/**
 * 消息头
 */
public final class Header {

    /**
     * 消息的校验码，3部分组成：
     *      0xabef0101 ：魔数，固定值，2个字节
     *      主版本号 ： 1个字节
     *      此版本号 ： 1个字节
     */
    private int crcCode = 0xabef0101;//魔数
    /**
     * 消息长度，包括消息头和消息体；4个字节
     *
     */
    private int length;// 消息长度
    /**
     * 回话ID，全局唯一；8个字节
     */
    private long sessionID;//会话ID
    /**
     * 消息类型（1个字节）：
     *      0：业务请求；
     *      1：业务响应；
     *      2：业务 one way（既是请求又是响应）；
     *      3：握手请求；
     *      4：握手应答；
     *      5：心跳请求；
     *      6：心跳应答；
     */
    private byte type; //消息类型
    /**
     * 消息优先级；1个字节
     */
    private byte priority; // 消息优先级
    /**
     * 消息头扩展字段，变长
     */
    private Map<String,Object> attachment = new HashMap<>();

    /**
     * @return the crcCode
     */
    public final int getCrcCode() {
        return crcCode;
    }

    /**
     * @param crcCode the crcCode to set
     */
    public final void setCrcCode(int crcCode) {
        this.crcCode = crcCode;
    }

    /**
     * @return the length
     */
    public final int getLength() {
        return length;
    }

    /**
     * @param length the length to set
     */
    public final void setLength(int length) {
        this.length = length;
    }

    /**
     * @return the sessionID
     */
    public final long getSessionID() {
        return sessionID;
    }

    /**
     * @param sessionID the sessionID to set
     */
    public final void setSessionID(long sessionID) {
        this.sessionID = sessionID;
    }

    /**
     * @return the type
     */
    public final byte getType() {
        return type;
    }

    /**
     * @param type the type to set
     */
    public final void setType(byte type) {
        this.type = type;
    }

    /**
     * @return the priority
     */
    public final byte getPriority() {
        return priority;
    }

    /**
     * @param priority the priority to set
     */
    public final void setPriority(byte priority) {
        this.priority = priority;
    }

    /**
     * @return the attachment
     */
    public final Map<String, Object> getAttachment() {
        return attachment;
    }

    /**
     * @param attachment the attachment to set
     */
    public final void setAttachment(Map<String, Object> attachment) {
        this.attachment = attachment;
    }

    /*
     * (non-Javadoc)
     *
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Header [crcCode=" + crcCode + ", length=" + length
                + ", sessionID=" + sessionID + ", type=" + type + ", priority="
                + priority + ", attachment=" + attachment + "]";
    }

}
